<template>
  <div>
    <el-upload
      class="avatar-uploader"
      action="https://jsonplaceholder.typicode.com/posts/"
      :show-file-list="false"
      :on-success="handleAvatarSuccess"
      :before-upload="beforeAvatarUpload"
      :http-request="upload"
    >
      <img v-if="value" :src="value" class="avatar">
      <i v-else class="el-icon-plus avatar-uploader-icon" />
      <!-- 遮罩层 -->
      <div v-if="value" class="cover" @click.stop>
        <!-- 预览 -->
        <i class="el-icon-zoom-in" @click="showPreview=true" />
        <!-- 删除 -->
        <i class="el-icon-delete" @click="$emit('input','')" />
      </div>
    </el-upload>
    <!-- 图片预览弹窗 -->
    <el-dialog title="预览" :visible.sync="showPreview">
      <img class="showPreview" :src="value" alt="">
    </el-dialog>
  </div>
</template>
<script>
import COS from 'cos-js-sdk-v5'
// SECRETID 和 SECRETKEY 请登录 https://console.cloud.tencent.com/cam/capi 进行查看和管理
const cos = new COS({
  SecretId: 'AKIDEICsNfkpqdO0NTKvGduZ4JJ3azAGDxUm',
  SecretKey: '80seicKwMbjhU2OZZN0SkGcrteKrHWjn'
})
export default {
  props: {
    // eslint-disable-next-line
    value:String
  },
  data() {
    return {
      showPreview: false
    }
  },
  methods: {
    handleAvatarSuccess(res, file) {
      this.value = URL.createObjectURL(file.raw)
    },
    beforeAvatarUpload(file) {
      const isLt2M = file.size / 1024 / 1024 < 2
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isLt2M
    },
    upload({ file }) {
      cos.uploadFile(
        {
          Bucket: 'hr-1316319910' /* 填写自己的 bucket，必须字段 */,
          Region: 'ap-guangzhou' /* 存储桶所在地域，必须字段 */,
          Key: file.name /* 存储在桶里的对象键（例如:1.jpg，a/b/test.txt，图片.jpg）支持中文，必须字段 */,
          Body: file, // 上传文件对象
          SliceSize:
            1024 *
            1024 *
            5 /* 触发分块上传的阈值，超过5MB使用分块上传，小于5MB使用简单上传。可自行设置，非必须 */,
          onProgress: function(progressData) {
            // console.log(JSON.stringify(progressData))
          }
        },
        (err, data) => {
          if (err) {
            console.log('上传失败', err)
          } else {
            // console.log(data)
            this.$emit('input', `http://${data.Location}`)
          }
        }
      )
    }
  }
}
</script>

<style scoped lang="scss">
.avatar-uploader ::v-deep .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  .cover {
    display: flex;
    align-items: center;
    justify-content: center;
    position: absolute;
    width: 100%;
    height: 100%;
    left: 0;
    bottom: 0;
    color: white;
    &:hover {
      background-color: rgba(0, 0, 0, 0.488);
      i {
        display: block;
      }
    }
    i {
      display: none;
      font-size: 28px;
    }
    i + i {
      margin-left: 20px;
    }
  }
}
.avatar-uploader ::v-deep .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
.showPreview{
  display: block;
  width: 100%;
}
</style>
